<template>
  <!-- 选择标签 -->
  <a-modal
    v-model:open="visible"
    width="800px"
    title="选择标签"
    :footer="null"
    :maskClosable="false"
    @cancel="cancel"
  >
    <div v-global-loading="modalloading">
      <div style="display: flex; justify-content: end; margin-bottom: 10px">
        <a-input-search
          allowClear
          v-model:value="pageSet.name"
          placeholder="请输入标签名称"
          enter-button
          @search="loadPagination(true)"
          style="width: 300px"
        />
      </div>
      <a-table
        size="small"
        :loading="tableLoading"
        :dataSource="tableData"
        :columns="columns"
        bordered
        :preserveSelectedRowKeys="true"
        :pagination="false"
        row-key="id"
        :row-selection="{
          selectedRowKeys: selectedRowKeysList,
          onChange: onSelectChange,
          preserveSelectedRowKeys: true
        }"
        :scroll="{
          x: 700,
          y: 500
        }"
      ></a-table>
      <Pagination
        :pageSet="pageSet"
        style="margin-top: 10px"
        :loadPagination="loadPagination"
      ></Pagination>
      <div style="display: flex; justify-content: end">
        <a-button style="margin-right: 14px" @click="addFormData(1)"
          >取消</a-button
        >
        <a-button type="primary" @click="addFormData(2)">确定</a-button>
      </div>
    </div>
  </a-modal>
</template>
<script lang="ts" setup>
import {ref, computed} from "vue";
import {tagsInfoPage} from "@/api/customerArchives/memberTag";
import Pagination from "@/components/pagination/Pagination.vue";
import {message, Modal} from "ant-design-vue";
const visible = ref<boolean>(false);
const modalloading = ref<boolean>(false);
const tableData = ref<Array<any>>([]);
const tableLoading = ref<boolean>(false);
// const selectedRowKeysList = ref([])
const props = defineProps({
  list: {
    type: Array,
    default: []
  },
  type: {
    type: Number,
    default: 2
  }
});
const type = computed({
  get() {
    return props.type;
  },
  set(value) {}
});
const emits = defineEmits(["update:list", "ok", "cancel"]);
const cancel = () => {
  emits("cancel");
};
const selectedRowKeysList = computed({
  get() {
    return props.list;
  },
  set(value) {
    emits("update:list", value);
  }
});
function onSelectChange(selectedRowKeys) {
  selectedRowKeysList.value = selectedRowKeys;
}
const columns = ref<Array<any>>([
  {
    title: "序号",
    dataIndex: "name",
    align: "center",
    width: 50,
    customRender: ({index}) => {
      return index + 1;
    },
    fixed: "left"
  },
  {
    title: "名称",
    dataIndex: "name",
    align: "center",
    width: 400
  }
  // {
  //   title: "标签类型",
  //   dataIndex: "tagType",
  //   align: "center",
  //   width: 200,
  //   customRender: ({ text }) => {
  //     return text == 1 ? "会员" : "商品";
  //   }
  // }
]);
const addFormData = (type: number) => {
  if (type == 1) {
    visible.value = false;
  } else {
    // if (selectedRowKeysList.value.length <= 0) {
    //   message.error("请选择标签！");
    // } else {
    emits("ok");
    // }
  }
};
const pageSet = ref<any>({
  pageNo: 1,
  pageSize: 20,
  total: 0,
  tagType: type.value
});
const loadPagination = (flag: boolean = false): void => {
  if (flag) {
    pageSet.value.tagType = type.value;
    pageSet.value.pageNo = 1;
  }
  tableLoading.value = true;
  tagsInfoPage(pageSet.value)
    .then((res) => {
      tableData.value = res.data.rows;
      pageSet.value.total = res.data.totalRows;
    })
    .finally(() => {
      tableLoading.value = false;
    });
};
const showModal = () => {
  pageSet.value.name = "";
  loadPagination(true);
  visible.value = true;
};
const nodeModal = () => {
  visible.value = false;
};
defineExpose({
  showModal,
  nodeModal,
  modalloading
});
</script>
<style lang="less" scoped></style>
